package com.yaokan.devices;

import android.annotation.SuppressLint;
import android.content.Context;
import android.hardware.ConsumerIrManager;
import android.util.Log;

public class ir {

	private static final String TAG = "IR";
	static ir instance;

	private native int transmitIR(Context ct, String encode); // Context对象, 密文 

	static {
		Log.e(TAG, "start load lib");
		System.loadLibrary("ir");
		Log.e(TAG, "end load");
	}

	// 测试发送红外信息: 密文
	public void irtest(Context ct){
		String encode="B8R[UUS2LEM8HGX4YL1C[8[L5]B2ED9QS6QN3]M0YT7[O4KT1CK8CJ5OO2ID9WU6KD3GS0IZ7]H4W\\1CU8EH5E[2MD9CW6ET3OW0SF7WC4IJ1CE8GF5UM2QD9IY6YJ3W[0CL7UW4UR1CO8ID5OW2UD9O[6SZ3EE0MR7SQ4GZ1CY8K\\5[K2YD9UC6MP3QH0WX7QK4SH1GG8MZ5QW2CD9[E6GF3YK0GD7OF4EP1HRE7WD4MG1CF8KC5UN2QC9IZ6YI3W\\0CK7UX4UQ1CP8IC5KZ2UC9O\\6SY3EF0MQ7SR4GY1CZ8K[5[L2YC9UD6QM3MJ0WW7QL4SG1CJ8MY5QX2CC9[F6GE3UN0GC7OF4EO1CT8OW5GJ2GC9GH6[U3CR0QI7MZ4QW1CD8QU5WV2KC9MJ6UK3KV0[O7KT4CE1CN8WR5MH";
		
		for(int i=0;i<1;i++){
			Log.e(TAG, "i:" + i);
			ir.transmit(encode, ct);
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public static ir getInstance() {
		if (instance == null) {
			instance = new ir();
		}
		return instance;
	}
	
	public static int transmit(String encode, Context ct) {
		Log.e(TAG, "Start");
		encode = encode.trim();
		int i = 0;
		if( encode.indexOf(",") >0 ){
			// 明文数据的处理
			getInstance().sendIR( encode, ct);
			i = 1;	// 假设为正确
		}else{
			// 密文数据的处理
			i = getInstance().transmitIR(ct, encode);
			/* 1: 成功
			 * -1: 失败， LogCat输出提示信息
			 * */ 
		}
		Log.e(TAG, "result: "+ i +" end");
		return i;
	}
	
	public  String int2Str(int _a[]){
		String s = "";
		for(int i=0;i<_a.length;i++){
			if( s.equals("") ) s = _a[i] + "";
			else s = s+"," + _a[i];
		}
		return s;
	}
	
	@SuppressLint("NewApi")
	public void sendIR(String plain, Context ct) {
		int data[] = String2Int(plain);
		int iSize = 0;
		
		if( data!=null ) iSize = data.length;
		if( iSize>3 ){
			int carrierFrequency = data[1];
			int pattern[] = new int[iSize - 2];
			String consumer_ir = "consumer_ir";
			
			System.arraycopy(data, 2, pattern, 0, pattern.length);
			Log.e(TAG, "cf:" + carrierFrequency);
			Log.e(TAG, "pattern:" + this.int2Str(pattern));
			
			ConsumerIrManager mCIR = (ConsumerIrManager) ct.getSystemService(consumer_ir);
			boolean hasIr = false;
			if( mCIR!=null ) {
				Log.e(TAG, "mCIR is not null");
				hasIr = mCIR.hasIrEmitter();
			}else
				Log.e(TAG, "mCIR is null");
			String str = "是否有红外设备: " + hasIr;
//			Toast(str);
			Log.e(TAG, str);
			if (hasIr) {
				str = "开始发送";
//				Toast(str);
				Log.e(TAG, str);
				mCIR.transmit(carrierFrequency, pattern);
				str = "发送结束";
//				Toast(str);
				Log.e(TAG, str);
			}else{
				str = "无红外设备，无法发送";
//				Toast(str);
				Log.e(TAG, str);
			}
		}
	}
	
	public int[] String2Int(String _s) {
		_s = _s.trim();
		String s[] = _s.split(",");
		int iLen = s.length;
		int aCMD[] = new int[iLen];
		for(int i=0;i<s.length;i++){
			aCMD[i] = this.CInt(s[i], 0);
		}
		return aCMD;
	}
	public int CInt(String str, int iDefault){
		int iR = iDefault;
		try{
			iR = Integer.parseInt(str);
		}catch(NumberFormatException e){
			iR = iDefault;
		}
		return iR;
	}
}
